<template>
  <div
      id="Sku"
      v-loading="loading"
  >
    <v-tableTitle
        :btnConfig="btnConfig"
        @del="del"
        @newCreate="newCreate"
        @exportFile="exportFile"
        @upFile="file"
    ></v-tableTitle>
    <v-table
        :height="'270px'"
        :page="page"
        :pageSize="pageSize"
        :tableConfig="tableConfig"
        :tableData="tableData"
        :total="total"
        @findData="getTableData"
        @getTableData="getTableData"
        @searhData="searhData"
        @selectData="selectData"
        @setdialog="setdialog"
        @upData="getTableData"
        ref="table"
    ></v-table>
  </div>
</template>

<script>
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import tableTitle from "@/components/commons/TableTitle";

export default {
  name: "SkuDetailReplenish",
  mixins: [baseMixin],
  props: {
    inputData: {
      type: Object
    }
  },
  data() {
    return {
      deleteStatus: true,
      loading: false,
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 10,
      tableSelectData: [],
      tableSearhData: {},
      tableConfig: [
        {
          label: this.$t('config.locType'),
          prop: "locationtype",
          input: "select",
          options: this.getOptions("querycodeLkup/LOCTYPE")
        },
        {
          label: this.$t('config.loc'),
          prop: "loc",
          input: "input"
        },
        {
          label: this.$t('config.qtylocationminimum'),
          prop: "qtylocationminimum",
          input: "input"
        },
        {
          label: this.$t('config.qtylocationlimit'),
          prop: "qtylocationlimit",
          input: "input"
        },
        {
          label: this.$t('config.replenishmentuom'),
          prop: "replenishmentuom",
          input: "select",
          options: this.getOptions("codelkup/EAIPCA")
        },
        {
          label: this.$t('config.replenishmentpriority'),
          prop: "replenishmentpriority",
          input: "input"
        },
        {
          label: this.$t('code.usable'),
          prop: "usable",
          input: "input",
          disabled: true
        },
        {
          label: this.$t('config.num'),
          prop: "qty",
          input: "input"
        },
        {
          label: this.$t('outbound.qtyallocated'),
          prop: "qtyallocated",
          input: "input"
        },
        {
          label: this.$t('outbound.qtypicked'),
          prop: "qtypicked",
          input: "input"
        }

      ],
      addflag: true
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.create'), certigier: this.newStatus, invalid: "newCreate" },
        { label: this.$t('common.delete'), certigier: this.deleteStatus, invalid: "del" },
        { type: "file1", label: this.$t('common.import'), invalid: "file" },
        { label: this.$t('common.export'), certigier: this.deleteStatus, invalid: "exportFile" }
      ];
    }
  },
  methods: {
    //导入
    async file(data) {
      try {
        let res = await this.$api.post("/importSkuxlocsingle", data);
        if (res.data.code == "ok") {
          this.getTableData(1, this.tableSearhData);
        } else {
          this.$message.error(res.data.desc);
        }
      } catch (e) {
        this.$message.error(e);
      }
    },
    //导出
    async exportFile() {
      let data = this.tableSelectData;
      try {
        let res = await this.$api.post("/exportSkuxlocsingle", data, { responseType: "blob" });
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, this.$t('config.allotLoc') + ".xls");
        } else {
          var blob = new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8" });
          var filename = this.$t('config.allotLoc') + ".xls";
          var downloadElement = document.createElement("a");
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = "none";
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    newCreate() {
      this.$parent.fpkwFlag = true;
      this.$parent.setMentActive("10");
      this.$parent.detailDataFpkw = {
        loc: "",
        replenishmentuom: "EA",
        qtyreplenishmentoverride: 0,
        locationtype: "OTHER",
        replenishmentpriority: "",
        qtylocationminimum: 0,
        qtylocationlimit: 0,
        allowreplenishfromcasepick: "",
        allowreplenishfrombulk: "",
        allowreplenishfrompiecepick: "",
        replenishmentseverity: "",
        qty: 0,
        qtypicked: 0,
        qtyexpected: 0,
        qtyallocated: 0
      };
    },
    async del() {
      let delFlag = false;
      this.tableSelectData.forEach(res => {
        if (res.qty > 0) {
          this.$message.error(this.$t('msg.msg8'));
          delFlag = true;
          return;
        }
        if (res.qtyallocated > 0) {
          this.$message.error(this.$t('msg.msg9'));
          delFlag = true;
          return;
        }
        if (res.qtypicked > 0) {
          this.$message.error(this.$t('msg.msg10'));
          delFlag = true;
          return;
        }
      });
      if (delFlag) {
        return;
      }
      try {
        let res = await this.$api.post(`/deleteSkuxlocsingle`, this.tableSelectData);
        
          this.$message.success(res.returnMgr);
          this.getTableData(1, {});
          this.$refs.table.delSelectData(this.tableSelectData);
        
      } catch (e) {
        this.$message.error(e);
      }

    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },
    //设置弹框
    setdialog(data) {
      this.$parent.fpkwFlag = false;
      this.$parent.setMentActive("10");
      this.$parent.detailDataFpkw = {
        ...data
      };

    },
    async getTableData(page, data) {
      this.$set(data, "storerkey", this.inputData.detailData.storerkey);
      this.$set(data, "sku", this.inputData.detailData.sku);
      try {
        this.loading = true;
        let res = await this.$api.post(`/querySkuxlocsingle/${page}`, data);
        
          this.tableData = res.page.list;
          this.page = res.page.curpage;
          this.pageSize = res.page.pageSize;
          this.total = res.page.total;
        
      } catch (e) {
        this.$message.error(error);
      } finally {
        this.loading = false;
      }
    }
  },
  watch: {
    tableSelectData(data) {
      if (data.length > 0) {
        this.deleteStatus = false;
      } else {
        this.deleteStatus = true;
      }
    }
  },
  mounted() {
    this.getTableData(1, {});
  },
  components: {
    "v-table": table,
    "v-tableTitle": tableTitle
  }
};
</script>

<style scoped>
#Sku {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
